﻿using Microsoft.Azure.Documents.Client;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Model.DocumentDB
{
    /// <summary>Base class for every view model using a <see cref="DocumentClient"/>.</summary>
    public abstract class DocumentClientViewModelBase
    {
        /// <summary>Constructor.</summary>
        /// <param name="client">Client shared among different models.</param>
        protected DocumentClientViewModelBase(DocumentClient client)
        {
            if (client == null)
            {
                throw new ArgumentNullException("client");
            }

            Client = client;
        }

        /// <summary>Construct a model from account credentials.</summary>
        /// <param name="id">ID of the account.</param>
        /// <param name="authKey">Authorization key to access the account.</param>
        protected DocumentClientViewModelBase(string id, string authKey)
            : this(CreateClient(id, authKey))
        {
        }

        /// <summary>Document client to the DocumentDB API.</summary>
        protected DocumentClient Client { get; private set; }

        private static DocumentClient CreateClient(string id, string authKey)
        {
            if (string.IsNullOrWhiteSpace(id))
            {
                throw new ArgumentNullException("id");
            }
            if (string.IsNullOrWhiteSpace(authKey))
            {
                throw new ArgumentNullException("authKey");

            }

            var endpoint = new Uri("https://" + id + ".documents.azure.com:443/");

            return new DocumentClient(endpoint, authKey);
        }
    }
}